package com.cheng.commonutlis.recyclerviewtest.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cheng.commonutlis.R;
import com.cheng.commonutlis.databinding.ActivitySimpleBinding;
import com.cheng.commonutlis.recyclerviewtest.adapter.DataAdapter;
import com.cheng.commonutlis.recyclerviewtest.app.BaseActivity;
import com.cheng.commonutlis.recyclerviewtest.bean.DataItemBean;
import com.cheng.commonutlis.recyclerviewtest.utils.DataUtil;
import com.cheng.commonutlis.recyclerviewtest.utils.ToastUtil;
import com.cheng.myrecyclerview.ByRecyclerView;
import com.cheng.myrecyclerview.decoration.SpacesItemDecoration;

/**
 * @author jingbin
 */
public class SimpleActivity extends BaseActivity<ActivitySimpleBinding> {

    private int page = 1;
    private DataAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        setTitle("基本使用");

        initAdapter();
    }

    private void initAdapter() {
        mAdapter = new DataAdapter(DataUtil.get(this, 6));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setRefreshEnabled(true);
        binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this).setHeaderNoShowDivider(1).setDrawable(R.drawable.shape_line));
        binding.recyclerView.setAdapter(mAdapter);
        binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (page == 3) {
                    binding.recyclerView.loadMoreEnd();
                    return;
                }
                page++;
                mAdapter.addData(DataUtil.getMore(SimpleActivity.this, 20, page));
                binding.recyclerView.loadMoreComplete();
            }
        }, 500);
        binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mAdapter.setNewData(DataUtil.get(SimpleActivity.this, 6));
            }
        }, 500);
        binding.recyclerView.setOnItemClickListener(new ByRecyclerView.OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                DataItemBean itemData = mAdapter.getItemData(position);
                ToastUtil.showToast(itemData.getTitle() + ": " + position);
                binding.recyclerView.setRefreshing(true);
            }
        });
    }
}
